<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('admin'.EXT);

class Promotion extends Admin {
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper('upload');
    }
    
    function update($id = null)
    {
        $row = $this->db->select()->from("promotion")->where('id',$id)->get()->row();
        
        $this->data['row']  =   $row;
        
        if($_SERVER['REQUEST_METHOD']=='POST')
        {
            $album_dir = 'uploads/promotions/';
            create_dir($album_dir);
            $config['upload_path'] = $album_dir;
    		$config['allowed_types'] = 'gif|jpg|png|swf';
    		$config['max_size']	= '100';
    		$config['max_width']  = '1024';
    		$config['max_height']  = '768';
            $config['encrypt_name'] = true;
            $this->load->library('upload', $config);
            
            $_data = array(
                'name'  =>  $this->input->post('name'),
                'url'  =>  $this->input->post('url'),
                'status'  =>  $this->input->post('status'),
                'created_date' => date('Y-m-d H:i:s'),
                'width'     => $this->input->post('width'),
                'height'     => $this->input->post('height')
            );
            if($row){
                if($_FILES['img']['name'] != '')
                {
                    if($this->upload->do_upload('img')){
                        $img = $this->upload->data();
                        $_data['source']    =   $album_dir.$img['file_name'];
                         $this->db->where('id',$row->id);
                        $this->db->update('promotion',$_data);
                        redirect(admin_url('promotion'));
                    }else{
                        $this->message->add('error', $this->upload->display_errors());
                    }
                }
                else
                {
                    $this->db->where('id',$row->id);
                    $this->db->update('promotion',$_data);
                    redirect(admin_url('promotion'));
                }
                
            }
            else{
                if($this->upload->do_upload('img')){
                    $img = $this->upload->data();
                    $_data['source']    =   $album_dir.$img['file_name'];
                    $this->db->insert('promotion',$_data);
                    redirect(admin_url('promotion'));
                }else{
                    $this->message->add('error', $this->upload->display_errors());
                }
            }
            //redirect(admin_url('config'));
        }
        
        $this->data['tpl_file']	= 'promotion/update';
        $this->load->view('layout/default', $this->data);
    }
    
    function index()
    {
        $this->data['promotions']   =   $this->db->select()->from("promotion")->where('status','yes')->get()->result();
        $this->data['tpl_file']	= 'promotion/index';
        $this->load->view('layout/default', $this->data);
    }
}